<?php
/**
* 
* ShopWindow Toolset
* 
* Copyright (C) 2008 Digital Window Ltd.
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
*/


require_once('class.soap_error.php');


/**
 * PHP5: Extends SoapClient and automatically configures settings specific to the AWin API
 *
 */
class soap_client extends SoapClient
{
	private static 	$oInstance 	= null;	// an instance of itself
	protected 		$oSoapError = null; // holds any errors produced during the SOAP requests


	/**
	 * The Constructor
	 *
	 * @copyright	DigitalWindow
	 * @author		Kostas Melas <kostas@digitalwindow.com>
	 *
	 * @access 	public
	 *
	 * @param 	object $oUser	the user object with login detail
	 */
	public function __construct($oUser)
	{
		// create client
		parent::__construct(API_WSDL, array('trace'=>API_SOAP_TRACE, 
											'compression'=> SOAP_COMPRESSION_ACCEPT | 
															SOAP_COMPRESSION_GZIP | 
															SOAP_COMPRESSION_DEFLATE) );


		// create headers
		$oHeader 	= new SoapHeader(API_NAMESPACE, 'UserAuthentication', $oUser, true, API_NAMESPACE);
		$oHeader2 	= new SoapHeader(API_NAMESPACE, 'getQuota', true, true, API_NAMESPACE);

		// set headers
		$this->__setSoapHeaders(array($oHeader, $oHeader2));

		// set WSDL caching
		ini_set("soap.wsdl_cache_enabled", 1);
		ini_set('soap.wsdl_cache_ttl', 86400);

		// set server response timeout
		ini_set('default_socket_timeout', 60);
	}



	/**
	 * Singleton function
	 *
	 * @author		Kostas Melas <kostas@digitalwindow.com>
	 *
	 * @access	public
	 *
	 * @param	object $oUser	the user object with login detail
	 * 
	 * @return	object 	an instance of the class
	 */
	public function &getInstance($oUser)
	{
		$sClassName =  __CLASS__;
		
		// only create new instance if necessary
		if ( !self::$oInstance instanceof $sClassName ) {
			self::$oInstance = new $sClassName($oUser);
		}
		
		return self::$oInstance;
	}



	/**
	 * Executes the speficied function from the WSDL
	 *
	 * @copyright	DigitalWindow
	 * @author		Kostas Melas <kostas@digitalwindow.com>
	 *
	 * @access 	public
	 *
	 * @param 	string 	$sFunctionName the name of the function to be executed
	 * @param 	mixed 	$mParams [optional] the parameters to be passed to the function, can be array or single value
	 *
	 * @return 	mixed 	the results or a SoapError object
	 */
	public function call($sFunctionName, $mParams='')
	{
		// catch any exceptions
		try {
			return $this->$sFunctionName($mParams);
		}
		catch(SoapFault $e) {
			$this->oSoapError = new soap_error($e, $sFunctionName);

			return $this->oSoapError;
		}
	}



	/**
	 * Gives the remaining operations quota
	 *
	 * @copyright	DigitalWindow
 	 * @author		Kostas Melas <kostas@digitalwindow.com>
	 *
	 * @access 	public
	 *
	 * @return 	int 	the remaining operations quota
	 */
	public function getQuota()
	{
		$sResponse= $this->__getLastResponse();

		// use R.Exp. rather than XML parsers, as they might not be installed
		preg_match('/getQuotaResponse>(.*)<\/.*:getQuotaResponse>/', $sResponse, $aMatches);

		$iQuota = $aMatches[1];

		return $iQuota;
	}

} // end class

?>